﻿using System.ComponentModel;
using Autofac;
using KJSyncXP.SQLite.Context; 

namespace KJSyncXP.Sqlite.Model
{
    public class Session : INotifyPropertyChanged
    {
        private User _loginUser;
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public User LoginUser
        {
            get => _loginUser;
            set
            {
                _loginUser = value;
                OnPropertyChanged("LoginUser");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>带密码的登录逻辑</summary>
        public bool Login(string username, string password)
        {
            // 实际需替换为“数据库验证用户密码”的逻辑
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            var user = sqliteCtx.Db.Select<User>().Where(u => u.Username == username && u.Userpwd == password).First();
            if (user != null)
            {
                 LoginUser = user;
                 return true;
            }
            return false; 
        }

        /*/// <summary>免密登录逻辑</summary>
        public bool Login(string username)
        {
            // 类似带密码登录，按需实现免密逻辑
            return false;
        }

        /// <summary>登出逻辑</summary>
        public void Logout()
        {
            LoginUser = null;
        }*/
    }
}